<?php

/**
 * lang actions.
 *
 * @package    sf_sandbox
 * @subpackage lang
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12479 2008-10-31 10:54:40Z fabien $
 */
class langActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	
  	
  	
  	
  	    $this->setDecorator();
  	    if(!$request->getParameter('page')){
  	    	$this->page = 1;
  	    }
  	    else {
  	    	$this->page=$request->getParameter('page');
  	    } 
  	
        $c = new Criteria();
        
        /*
         * Поиск
         */
        if($request->getParameter('lang_filters[target]'))
        {
        	$c->add(LangPeer::TITLE,'%'.$request->getParameter('lang_filters[target]').'%',Criteria::LIKE);
        }
        
        if($request->getParameter('lang_filters[name_'.$this->getUser()->getCulture().']'))
        {
          $c->add(LangI18nPeer::NAME,'%'.$request->getParameter('lang_filters[name_'.$this->getUser()->getCulture().']').'%',Criteria::LIKE) ; 
        }
        
       
                
        
        
        $c->addDescendingOrderByColumn(LangPeer::ID);
  	    $pager = new sfPropelPager ( 'Lang', 50 );
		$pager->setCriteria ( $c );
		$pager->setPeerMethod ( 'doSelectWithI18n' );
		$pager->setPage ( $this->page );
		$pager->init ();
		$this->tr = $pager;
  	
  }
  
  
public function executeDelete(sfWebRequest $request)
  {

  	$this->setDecorator();
  	$m = LangPeer::retrieveByPK($request->getParameter('id'));
  	$m->delete();
    $this->redirect('lang/index'.$_POST['p']);
    
  }
  
  public function executeEdit(sfWebRequest $request)
  {
  	$this->setDecorator();
  	$m = LangPeer::retrieveByPK($request->getParameter('id'));
  	$this->form = new LangForm($m);
  	
    if ($request->isMethod('post'))
    {

      $this->form->bind($request->getParameter('lang'),$request->getFiles('lang'));
      if ($this->form->isValid())
      {
      	
        $this->form->save();
       $this->redirect('lang/index'.$_POST['p']);
       
      }
    }
    
  }
  
  
public function executeExport () {

		// определяем колво и наименовнаие языков
		$i = 0;
		$_arr_lang=array();
		while ( sfConfig::get("app_lang_".$i) )
	    {
	    	$_arr_lang[]=sfConfig::get("app_lang_".$i);
		    $i++;
	    }		
	
	    // бежим по циклу
		for ($i=0; $i<count($_arr_lang); $i++){    	
			
			$_name_lang=$_arr_lang[$i];
			$zap="";
		    
			$c = new Criteria();
			$items = LangPeer::doSelectWithI18n($c, $_name_lang);
		  	
$zap.='<?xml version="1.0" ?>
<xliff version="1.0">
	<file orginal="global" source-language="'.$_name_lang.'" datatype="plaintext">
    	<body>
';
			
		  	
		  	foreach($items as $it)
		  	{
		  		//$ar[$it->getId()]=$it->getName()."-".$it->getTitle();
$zap.='
			<trans-unit id="3">
        		<source>'.$it->getTitle().'</source>
        		<target>'.$it->getName().'</target>
			</trans-unit>
';
		  	}
		  	
$zap.='
		</body>
	</file>
</xliff>';
		  	
		  	$file_name=sfConfig::get('sf_app_i18n_dir')."/messages.".$_name_lang.".xml";
		  	//$file_name2=sfConfig::get('sf_apps_dir')."/".sfConfig::get('app_name')."/i18n/messages.".$_name_lang.".xml";
		  	//$file_name3=sfConfig::get('sf_apps_dir')."/".sfConfig::get('app_name')."/i18n/".sfConfig::get('app_name')."/messages.".$_name_lang.".xml";
		  	$this->write_file($file_name, $zap);
		  	//$this->write_file($file_name2, $zap);
		  	//$this->write_file($file_name3, $zap);
		  	sfToolkit::clearGlob(sfConfig::get('sf_cache_dir'));

		}    
	    
		$this->setLayout (false);
		
	} 
	 
	 
	 
	 public function write_file($file_name, $zap){
	
		// открываем его
		$file=fopen($file_name,"w+");
	
		// пишем в него
		//$zap.=date('d.m.Y H-i-s')."\n";
		fputs($file, $zap);
		fclose($file);
	
	}	
	
	public function executeTranslate (sfWebRequest $request) {
		
		
	   if ($request->getParameter('direction')=="en-ru") 
      {
         $lang = 'ru';
      }
      else 
      {
      	 $lang = 'en';
      }
      

      
		$l = LangPeer::retrieveByPK($request->getParameter('id'));
		
		$this->message = $this->getUser()->translate($l->getTitle(),$request->getParameter('direction'));
		
		$l->setName($this->message,$lang);
		$l->save();
	}
	
	
	public function setDecorator()
	{
		
		$module = $this->getRequest()->getParameter('module');
		$action = $this->getRequest()->getParameter('action');
		
		$path_layout = sfConfig::get('sf_app_template_dir').'/layouts/'.sfConfig::get('app_name').'/'.$module.'.php';
		$path_template = sfConfig::get('sf_app_module_dir').'/'.$module.'/templates/'.sfConfig::get('app_name').'/'.$action.'Success.php';
		if(file_exists($path_layout))
		{ $l = 'layouts/'.sfConfig::get('app_name').'/'.$module; }
		else
		{ $l = sfConfig::get('app_name').'_layout'; }
		if(file_exists($path_template))
		{ $t = sfConfig::get('app_name').'/'.$action; }
		else
		{ $t = $action; }
			
	/*
     * Установка layout
     */	
			$this->setLayout ( $l );
		
     /*
     *  Устанавливаем шаблон
     */
		
			$this->setTemplate ( $t );
	}
	
  public function executeSetlang(sfWebRequest $request)
  {
              if($this->getUser()->isAuthenticated())
		    {
		    	$pf = $this->getContext()->getUser()->getProfile();
		    	$pf->setLanguage('en');
		    	$pf->save();	       
		    }
	if($request->getParameter('lang')=='ru')
	{
		  $this->getUser()->setCulture('ru');
	}
	else 
	{
		  $this->getUser()->setCulture('en');
	}	    
   $refer = $this->getRequest()->getReferer();
   $refer = str_replace('lang/ru','',$refer);
   $refer = str_replace('lang/en','',$refer);
   //echo $refer;
   //die;
   //$this->redirect($refer);
   $this->redirect('@home');
  }
 
  
}
